package com.qianxx.base.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public final class ScreenUtils {

	private ScreenUtils() {
	};

	public static int getScreenWitdh(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		display.getMetrics(metrics);
		return metrics.widthPixels;
	}

	public static int getScreenHeight(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		display.getMetrics(metrics);
		return metrics.heightPixels;
	}
	
	private static float defaultDensity = 1.5f;// 高分辨率的手机density普遍接近1.5

	/**
	 * 根据手机的分辨率 dp 转成px(像素)
	 * */
	public static int dip2px(float dpValue, Context context) {
		int px;
		float density = context.getResources().getDisplayMetrics().density;
		if (density == 0) {
			px = (int) (dpValue * defaultDensity + 0.5f);
		} else {
			px = (int) (dpValue * density + 0.5f);
		}
		return px;
	}

	/**
	 * 根据手机的分辨率px(像素) 转成dp
	 */
	public static int px2dip(float pxValue, Context context) {
		int dp;
		float density = context.getResources().getDisplayMetrics().density;
		if (density == 0) {
			dp = (int) (pxValue / defaultDensity + 0.5f);
		} else {
			dp = (int) (pxValue / density + 0.5f);
		}
		return dp;
	}
}
